package com.project.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.project.bean.CoachBean;
import com.project.bean.LessonBean;
import com.project.bean.OrderBean;
import com.project.bean.PageBean;
import com.project.bean.StudentBean;
import com.project.dao.CoachMapper;
import com.project.dao.LessonMapper;
import com.project.dao.OrderMapper;
import com.project.dao.studentMapper;
import com.project.service.IOrderService;
@Service
public class OrderServiceImpl implements IOrderService{
	@Autowired
	private OrderMapper orderdao;
	@Autowired
	private LessonMapper lessonMapper;
	@Autowired
	private studentMapper studentMapper; 
	@Autowired
	private CoachMapper coachMapper;
	@Override
	public OrderBean findById(int orderId) {
		OrderBean order=orderdao.selectByPrimaryKey(orderId);
		return order;
	}

	@Override
	public int addOrder(OrderBean order) {
		LessonBean lesson = lessonMapper.selectByPrimaryKey(order.getoLId());
		lesson.setlState(4);
		lessonMapper.updateByPrimaryKey(lesson);
		order.setoPrice(lesson.getlPrice());
		int num=orderdao.insertSelective(order);
		return num;
	}
	@Override
	public int refuseOrder(int orderId) {
		LessonBean lessonBean = orderdao.selectByPrimaryKey(orderId).getLessonBean();
		lessonBean.setlState(1);
		lessonMapper.updateByPrimaryKey(lessonBean);
		int num = orderdao.deleteByPrimaryKey(orderId);
		return num;
	}
	@Override
	public int changeState(OrderBean order) {
		int state = order.getoState();
		LessonBean lessonBean = orderdao.selectByPrimaryKey(order.getoId()).getLessonBean();
		if(state==3) {
			lessonBean.setlState(1);
			lessonMapper.updateByPrimaryKey(lessonBean);
		}else if(state==2) {
			lessonBean.setlState(2);
			lessonMapper.updateByPrimaryKey(lessonBean);
		} else /* if(state==4) */ {
			lessonBean.setlState(3);
			lessonMapper.updateByPrimaryKey(lessonBean);
		}

		int num=orderdao.updateByPrimaryKeySelective(order);
		return num;
	}
	@Override
	public int payOrder(OrderBean order) {
		int num = orderdao.updateByPrimaryKeySelective(order);
		return num;
	}
	@Override
	public int completeOrder(OrderBean order) {
		int num = 0;
		//此处可优化，只选择id和余额，并选择性修改余额
		StudentBean stu = studentMapper.selectByUserId(order.getoUId1());
		CoachBean coa = coachMapper.findByUid(order.getoUId2());
		double money1 = stu.getsMoney()-order.getoPrice();
		stu.setsMoney(money1);
		double money2 = coa.getcMoney()+order.getoPrice();
		coa.setcMoney(money2);
		if(money1>0) {
			studentMapper.updateByPrimaryKey(stu);
			coachMapper.updateByPrimaryKey(coa);
			num = orderdao.updateByPrimaryKey(order);
		}
		return num;
	}

	@Override
	public PageBean findAllToPageByStu(int sId, int page, int size, int state) {
		PageBean pageBean=new PageBean();
		Map<String,Integer> map=new HashMap<String,Integer>();
		map.put("sId", sId);
		map.put("page", (page-1)*size);
		map.put("size", size);
		map.put("state", state);
		List<OrderBean> list=orderdao.findAllToPageByStu(map);
		int num=orderdao.findAllNumByStu(map);
		int totalPage=(num%size==0)?(num/size):(num/size+1);
		pageBean.setTotalNumber(num);
		pageBean.setTotalPage(totalPage);
		pageBean.setSize(size);
		pageBean.setPage(page);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public PageBean findAllToPageByCoach(int cId, int page, int size, int state) {
		PageBean pageBean=new PageBean();
		Map<String,Integer> map=new HashMap<String,Integer>();
		map.put("cId", cId);
		map.put("page", (page-1)*size);
		map.put("size", size);
		map.put("state", state);
		List<OrderBean> list=orderdao.findAllToPageByCoach(map);
		int num=orderdao.findAllNumByCoach(map);
		int totalPage=(num%size==0)?(num/size):(num/size+1);
		pageBean.setTotalNumber(num);
		pageBean.setTotalPage(totalPage);
		pageBean.setSize(size);
		pageBean.setPage(page);
		pageBean.setList(list);
		return pageBean;
		
	}


}
